//
//  AKRequest.h
//  CocoaBean
//
//  Created by sleeper on 6/3/10.
//  Copyright 2010 __MyCompany__ All rights reserved.
//

@class GDataOAuthAuthentication;
@protocol AKRequestDelegate;

@interface AKRequest : NSOperation {
	id _delegate;
	GDataOAuthAuthentication *_auth;
	BOOL _usesJson;
	NSMutableDictionary *_parameters;
	NSString *_postContent;
	
	NSString *_identifier;
	NSUInteger _tag;
	NSURL *_url;
	NSString *_httpMethod;
	NSString *_type;
	BOOL _appendsJsonParam;
	NSString *_uuid;
	
	struct {
		unsigned int delegateRespondsToDidFinish:1;
		unsigned int delegateRespondsToDidFail:1;
		unsigned int delegateRespondsToReceiveOnMainThread:1;
		unsigned int connectionFinished:1;
	} _requestFlags;
	
	// Connection
	NSURLConnection *_connection;
	NSMutableData *_receivedData;
}

@property (nonatomic, retain) NSURL *url;
@property (nonatomic, copy) NSMutableDictionary *parameters;
@property (nonatomic, copy) NSString *httpMethod;
@property (nonatomic, copy) NSString *postContent;
@property (nonatomic, copy, readonly) NSString *type;

@property (nonatomic, assign) BOOL appendsJsonParam;	// default to YES

@property (nonatomic, assign) id <AKRequestDelegate> delegate;

// Identifying a request
@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, assign) NSUInteger tag;

- (void)addIntParameter:(NSUInteger)intParam forKey:(NSString *)key;

@end

@protocol AKRequestDelegate <NSObject>
@optional
- (BOOL)calledOnMainThreadForRequest:(AKRequest *)request;	// if not implemented, default to send methods on main thread.
- (void)request:(AKRequest *)request didFinishWithData:(NSData *)data;
- (void)request:(AKRequest *)reqeust didFailWithError:(NSError *)error;
@end

extern NSString * const kAKRequestParameterKeyStartIndex;
extern NSString * const kAKRequestParameterKeyMaxResults;